<template>
  <el-dialog
    :title="model == 'add' ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <!-- <el-form-item label="VIN码" prop="vincode">
        <el-input v-model="dataForm.vincode" placeholder="VIN码"></el-input>
      </el-form-item> -->
      <!-- <el-form-item label="发动机号" prop="engineCode">
        <el-input
          v-model="dataForm.engineCode"
          placeholder="发动机号"
        ></el-input>
      </el-form-item> -->
      <!-- <el-form-item label="车架号" prop="chassisCode">
        <el-input
          v-model="dataForm.chassisCode"
          placeholder="车架号"
        ></el-input>
      </el-form-item> -->
      <el-form-item label="终端 ID" prop="terminalId">
        <el-input
          :disabled="model == 'edit'"
          v-model="dataForm.terminalId"
          placeholder="终端 ID"
        ></el-input>
      </el-form-item>
      <el-form-item label="车辆品牌" prop="productType">
        <el-input
          v-model="dataForm.productType"
          placeholder="车辆品牌"
        ></el-input>
      </el-form-item>
      <el-form-item label="车辆型号" prop="modelType">
        <el-input
          v-model="dataForm.modelType"
          placeholder="车辆型号"
        ></el-input>
      </el-form-item>
      <el-form-item label="车牌号" prop="carNumber">
        <el-input v-model="dataForm.carNumber" placeholder="车牌号"></el-input>
      </el-form-item>
      <!-- <el-form-item label="生产日期" prop="productDate">
        <el-input
          v-model="dataForm.productDate"
          placeholder="生产日期"
        ></el-input>
      </el-form-item> -->
      <!-- <el-form-item label="购买日期" prop="buyDate">
        <el-input v-model="dataForm.buyDate" placeholder="购买日期"></el-input>
      </el-form-item> -->
      <!-- <el-form-item label="工况时间" prop="workdatatime">
        <el-input
          v-model="dataForm.workdatatime"
          placeholder="工况时间"
        ></el-input>
      </el-form-item> -->
      <!-- <el-form-item label="经度" prop="lng">
        <el-input v-model="dataForm.lng" placeholder="经度"></el-input>
      </el-form-item>
      <el-form-item label="纬度" prop="lat">
        <el-input v-model="dataForm.lat" placeholder="纬度"></el-input>
      </el-form-item> -->
      <!-- <el-form-item label="速度(公里/小时)" prop="speed">
        <el-input
          v-model="dataForm.speed"
          placeholder="速度(公里/小时)"
        ></el-input>
      </el-form-item>
      <el-form-item label="累计油耗(L)" prop="totalenginefuel">
        <el-input
          v-model="dataForm.totalenginefuel"
          placeholder="累计油耗(L)"
        ></el-input>
      </el-form-item>
      <el-form-item label="累计工时(小时)" prop="totalenginehours">
        <el-input
          v-model="dataForm.totalenginehours"
          placeholder="累计工时(小时)"
        ></el-input>
      </el-form-item>
      <el-form-item label="累计里程(公里)" prop="totalmileage">
        <el-input
          v-model="dataForm.totalmileage"
          placeholder="累计里程(公里)"
        ></el-input>
      </el-form-item> -->
      <!-- <el-form-item label="机油压力(KPa)" prop="engineoilpressure">
        <el-input
          v-model="dataForm.engineoilpressure"
          placeholder="机油压力(KPa)"
        ></el-input>
      </el-form-item>
      <el-form-item label="发动机水温(度)" prop="enginewatertemperature">
        <el-input
          v-model="dataForm.enginewatertemperature"
          placeholder="发动机水温(度)"
        ></el-input>
      </el-form-item>
      <el-form-item label="发动机转速(转/分钟)" prop="enginespeed">
        <el-input
          v-model="dataForm.enginespeed"
          placeholder="发动机转速(转/分钟)"
        ></el-input>
      </el-form-item> -->
      <!-- <el-form-item label="在线状态(1在线0离线)" prop="onlinestate">
        <el-input
          v-model="dataForm.onlinestate"
          placeholder="在线状态(1在线0离线)"
        ></el-input>
      </el-form-item>
      <el-form-item label="创建时间" prop="createtime">
        <el-input
          v-model="dataForm.createtime"
          placeholder="创建时间"
        ></el-input>
      </el-form-item>
      <el-form-item label="更新时间" prop="updatetime">
        <el-input
          v-model="dataForm.updatetime"
          placeholder="更新时间"
        ></el-input>
      </el-form-item> -->
      <!-- <el-form-item label="" prop="pkey">
        <el-input v-model="dataForm.pkey" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="" prop="pvalue">
        <el-input v-model="dataForm.pvalue" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="" prop="pkey1">
        <el-input v-model="dataForm.pkey1" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="" prop="pvalue1">
        <el-input v-model="dataForm.pvalue1" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="" prop="pkey2">
        <el-input v-model="dataForm.pkey2" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="" prop="pvalue2">
        <el-input v-model="dataForm.pvalue2" placeholder=""></el-input>
      </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getObj, addObj, putObj } from "@/api/dbdvehicle";

export default {
  data() {
    return {
      visible: false,
      model:'add',
      dataForm: {
        terminalId: '',
        vincode: "",
        engineCode: "",
        chassisCode: "",
        productType: "",
        modelType: "",
        carNumber: "",
        productDate: "",
        buyDate: "",
        workdatatime: "",
        lng: "",
        lat: "",
        speed: "",
        totalenginefuel: "",
        totalenginehours: "",
        totalmileage: "",
        engineoilpressure: "",
        enginewatertemperature: "",
        enginespeed: "",
        onlinestate: "",
        createtime: "",
        updatetime: "",
        // pkey: "",
        // pvalue: "",
        // pkey1: "",
        // pvalue1: "",
        // pkey2: "",
        // pvalue2: "",
      },
      dataRule: {
        terminalId: [
          { required: true, message: "终端 ID 不能为空", trigger: "blur" },
        ],
        vincode: [
          { required: true, message: "VIN码不能为空", trigger: "blur" },
        ],
        engineCode: [
          { required: true, message: "发动机号不能为空", trigger: "blur" },
        ],
        chassisCode: [
          { required: true, message: "车架号不能为空", trigger: "blur" },
        ],
        productType: [
          { required: true, message: "车辆品牌不能为空", trigger: "blur" },
        ],
        modelType: [
          { required: true, message: "车辆型号不能为空", trigger: "blur" },
        ],
        carNumber: [
          { required: true, message: "车牌号不能为空", trigger: "blur" },
        ],
        productDate: [
          { required: true, message: "生产日期不能为空", trigger: "blur" },
        ],
        buyDate: [
          { required: true, message: "购买日期不能为空", trigger: "blur" },
        ],
        workdatatime: [
          { required: true, message: "工况时间不能为空", trigger: "blur" },
        ],
        lng: [{ required: true, message: "经度不能为空", trigger: "blur" }],
        lat: [{ required: true, message: "纬度不能为空", trigger: "blur" }],
        speed: [
          {
            required: true,
            message: "速度(公里/小时)不能为空",
            trigger: "blur",
          },
        ],
        totalenginefuel: [
          { required: true, message: "累计油耗(L)不能为空", trigger: "blur" },
        ],
        totalenginehours: [
          {
            required: true,
            message: "累计工时(小时)不能为空",
            trigger: "blur",
          },
        ],
        totalmileage: [
          {
            required: true,
            message: "累计里程(公里)不能为空",
            trigger: "blur",
          },
        ],
        engineoilpressure: [
          { required: true, message: "机油压力(KPa)不能为空", trigger: "blur" },
        ],
        enginewatertemperature: [
          {
            required: true,
            message: "发动机水温(度)不能为空",
            trigger: "blur",
          },
        ],
        enginespeed: [
          {
            required: true,
            message: "发动机转速(转/分钟)不能为空",
            trigger: "blur",
          },
        ],
        onlinestate: [
          {
            required: true,
            message: "在线状态(1在线0离线)不能为空",
            trigger: "blur",
          },
        ],
        createtime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" },
        ],
        updatetime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" },
        ],
        // pkey: [{ required: true, message: "不能为空", trigger: "blur" }],
        // pvalue: [{ required: true, message: "不能为空", trigger: "blur" }],
        // pkey1: [{ required: true, message: "不能为空", trigger: "blur" }],
        // pvalue1: [{ required: true, message: "不能为空", trigger: "blur" }],
        // pkey2: [{ required: true, message: "不能为空", trigger: "blur" }],
        // pvalue2: [{ required: true, message: "不能为空", trigger: "blur" }],
      },
    };
  },
  methods: {
    init(id) {
      this.dataForm.terminalId = id || '';
      this.model = id ? 'edit' : 'add'
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (id) {
          getObj(id).then((response) => {
            this.dataForm = response.data.result;
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          if (this.dataForm.terminalId && this.model == 'edit') {
            putObj(this.dataForm).then(() => {
              this.$message.success("修改成功");
              this.visible = false;
              this.$emit("refreshDataList");
            });
          } else {
            addObj(this.dataForm).then(() => {
              this.$message.success("添加成功");
              this.visible = false;
              this.$emit("refreshDataList");
            });
          }
        }
      });
    },
  },
};
</script>
